﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace tcp_SocketServer_10
{
    class Client
    {
        private Socket clientSocket;

        public string message;

        byte[] data = new byte[1024];

        public Client(Socket s)
        {
            clientSocket = s;


            new Thread(ReciveMessage).Start();
        }


        public void ReciveMessage()
        {
            while (true)
            {
               if (clientSocket.Poll(10,SelectMode.SelectRead))
               {
                   clientSocket.Close();
                   break;
               }
               int length = clientSocket.Receive(data);
               message = Encoding.UTF8.GetString(data, 0, length);

               Program.BoMessage(message);
            }

        }


        public void SendMessage(string str)
        {
            byte[] data2 = new byte[1024];
            data2 = Encoding.UTF8.GetBytes(str);
            clientSocket.Send(data2);

        }

        public bool IsConnected
        {
            get { return clientSocket.Connected; }
        }
    }
}
